<template>
    <div class="login-wrapper">
      <div class="login-container">
        <h2>登录</h2>
        <form @submit.prevent="handleSubmit">
          <div class="form-group">
            <label for="phone">手机号码</label>
            <input
              type="text"
              id="phone"
              v-model="phone"
              placeholder="请输入手机号码"
              required
            />
          </div>
          <div class="form-group">
            <label for="pwd">密码</label>
            <input
              type="password"
              id="pwd"
              v-model="pwd"
              placeholder="请输入密码"
              required
            />
          </div>
          <button type="submit">登录</button>
        </form>
        <p v-if="errorMessage" class="error-message">{{ errorMessage }}</p>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref } from 'vue';
  import { useRouter } from 'vue-router';
  import axios from '../plugins/axios'; // 引入你定义的 axios 实例
  
  const phone = ref('');
  const pwd = ref('');
  const errorMessage = ref('');
  const router = useRouter();
  
  const handleSubmit = async () => {
    if (!phone.value || !pwd.value) {
      errorMessage.value = '手机号码和密码不能为空';
      return;
    }
  
    try {
      // 发送登录请求
      const response = await axios.post('/user/login', {
        phone: phone.value,
        pwd: pwd.value,
      });
  
      console.log(response.data);
  
      if (response.data.code) {
        // 登录成功
        localStorage.setItem('realname', response.data.realname); // 存储用户信息
        localStorage.setItem('userId', response.data.userId); // 存储用户信息
        localStorage.setItem('token', response.data.data); // 存储 token
        localStorage.setItem('isAdmin', response.data.isAdmin); // 存储 isAdmin
        localStorage.setItem('role', response.data.role); // 存储 role
        const resolvedUsername = response.data.username || response.data.realname || '';
        if (resolvedUsername) {
          localStorage.setItem('username', resolvedUsername);
        } else {
          localStorage.removeItem('username');
        }
        if (response.data.email) {
          localStorage.setItem('userEmail', response.data.email);
        } else {
          localStorage.removeItem('userEmail');
        }
        router.push('/').then(() => {
            window.location.reload();
        });
 // 跳转到首页
      } else {
        // 登录失败
        errorMessage.value = '手机号码或密码错误';
      }
    } catch (error) {
      console.error('登录请求失败:', error);
      if (error.response) {
        console.error('响应错误:', error.response.data);
        errorMessage.value = error.response.data.message || '登录失败，请稍后再试';
      } else {
        errorMessage.value = '登录失败，请稍后再试';
      }
    }
  };
  </script>
  
  <style scoped>
  /* 样式保持不变 */
  body {
    font-family: Arial, Helvetica, sans-serif;
    color: #333;
  }
  
  .login-wrapper {
    display: flex;
    justify-content: center;
    align-items: center;
    height: calc(100vh - 120px);
  }
  
  .login-container {
    width: 100%;
    max-width: 300px;
    padding: 20px;
    border: 1px solid #ccc;
    border-radius: 5px;
    background-color: #fff;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    overflow: hidden;
  }
  
  h2 {
    text-align: center;
    margin-bottom: 15px;
    font-size: 1.5rem;
    font-weight: bold;
    color: #333;
  }
  
  .form-group {
    margin-bottom: 10px;
  }
  
  label {
    display: block;
    margin-bottom: 5px;
    font-weight: bold;
    font-size: 1rem;
    color: #666;
  }
  
  input {
    width: 100%;
    padding: 8px;
    box-sizing: border-box;
    border: 1px solid #ccc;
    border-radius: 4px;
  }
  
  button {
    width: 100%;
    padding: 10px;
    background-color: #28a745;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 1rem;
  }
  
  button:hover {
    background-color: #218838;
  }
  
  .error-message {
    color: red;
    margin-top: 10px;
    text-align: center;
  }
  </style>
